/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.KdniaoTrackQueryAPI;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.goods.entity.ZtWl;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztWl")
public class ZtWlController extends BaseController {

	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtWl get(String id, boolean isNewRecord) {

		ZtWl ztWl = new ZtWl();
		
		return ztWl;
	}

	/**
	 * 查询列表
	 */
	
	@RequestMapping(value = { "list", "" })
	public String list(ZtWl ztWl, Model model) {
		model.addAttribute("ztWl", ztWl);
		model.addAttribute("state", "");
		return "modules/ztWl/ztWlList";
	}

	/**
	 * 查询列表数据
	 */

	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtWl> listData(ZtWl ztWl,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<ZtWl> page = new Page<ZtWl>();
		ztWl.setPage(page);
		
	//	page.setList();
		List<ZtWl> ztwlList=new ArrayList<ZtWl>(); 
		KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
		String result = "";
		try {
			System.out.println(ztWl.getLogisticsCode()+"---"+ ztWl.getLogistics());
			result = api.getOrderTracesByJson(ztWl.getLogisticsCode(), ztWl.getLogistics());

		} catch (Exception ex) {
			ex.printStackTrace();
			
		}
		if(StringUtils.isNotBlank(result)){
			JSONObject jo = JSONObject.parseObject(result);
			String state = jo.getString("State");
			if("0".equals(state)){
				model.addAttribute("state", "无物流信息");
			}else if("2".equals(state)){
				model.addAttribute("state", "配送中");
			}else if("4".equals(state)){
				model.addAttribute("state", "拒绝签收");
			}else if("3".equals(state)){
				model.addAttribute("state", "已签收");
			}
			JSONArray ja=jo.getJSONArray("Traces");
			if(jo!=null && jo.size()>0){
				for(int i=0;i<ja.size();i++){
					JSONObject jj=ja.getJSONObject(i);
				ZtWl ztwl=new ZtWl(); 
				try {
					ztwl.setCreateDate(DateUtils.parseDate(jj.get("AcceptTime").toString(),"yyyy-MM-dd HH:mm:ss"));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ztwl.setTitle(jj.get("AcceptStation").toString());
				ztwlList.add(ztwl);
				}
			}
			page.setList(ztwlList);
		}

		return page;
	}
	
	

	

	
}